FROM gradle:jdk11
USER root

# Install Build Essentials
RUN apt-get update && apt-get install -y build-essential file apt-utils git

RUN mkdir -p /cache-jdk11 && chmod +rwx /cache-jdk11
RUN find /cache-jdk11 -type f -name "*.lock" -delete
ENV GRADLE_USER_HOME=/cache-jdk11
VOLUME $GRADLE_USER_HOME

WORKDIR /app

ARG cmdline_tools=https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip
ARG android_home=/usr/local/android-sdk


# Download and install Android Commandline Tools
RUN  mkdir -p ${android_home}/cmdline-tools && \
    wget -O /tmp/cmdline-tools.zip -t 5 "${cmdline_tools}" && \
    unzip -q /tmp/cmdline-tools.zip -d ${android_home}/cmdline-tools && \
    mv ${android_home}/cmdline-tools/cmdline-tools ${android_home}/cmdline-tools/latest && \
    rm /tmp/cmdline-tools.zip


ENV ANDROID_HOME ${android_home}
ENV ANDROID_SDK_ROOT ${android_home}
ENV ADB_INSTALL_TIMEOUT 120
RUN pwd
RUN ls -l
ENV PATH=${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/tools:${ANDROID_SDK_ROOT}/tools/bin:${ANDROID_SDK_ROOT}/platform-tools:${PATH}

RUN mkdir ~/.android && echo '### User Sources for Android SDK Manager' > ~/.android/repositories.cfg

RUN yes | sdkmanager --licenses && yes | sdkmanager --update

# Update SDK manager and install system image, platform and build tools
RUN sdkmanager \
  "tools" \
  "platform-tools"

RUN sdkmanager \
"build-tools;28.0.3" \
 "build-tools;29.0.2" \
 "build-tools;30.0.2"
 RUN sdkmanager "platforms;android-28"
 RUN sdkmanager "platforms;android-30"


#ARG UNAME=jenkins
#ARG UID=1000
#ARG GID=1000
#RUN groupadd -g $GID -o $UNAME
#RUN useradd -m -u $UID -g $GID -o -s /bin/bash $UNAME
#RUN chown -R $UNAME.$UNAME /cache
#USER $UNAME
